package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseSaleAttr;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SpuSaveInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Api("spu控制")
@RestController
//http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
@RequestMapping("admin/product")
public class SpuController {
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @ApiOperation("查询spu定义的所有销售属性名和值")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId){

        List<SpuSaleAttr>  saleAttrs = spuSaleAttrService
                .getSpuSaleAttrList(spuId);
        return Result.ok(saleAttrs);
    }

    @ApiOperation("Spu分页查询")
    @GetMapping("/{page}/{limit}")
    public Result sputList(@PathVariable("page") Integer pn,
                           @PathVariable("limit") Integer ps,
                           @PathParam("category3Id")Long category3Id){

        Page<SpuInfo> page = spuInfoService.lambdaQuery().eq(SpuInfo::getCategory3Id, category3Id)
                .page(new Page<>(pn, ps));

        return Result.ok(page);

    }
    @PostMapping("/saveSpuInfo")
    @ApiOperation("添加spu")
    public Result saveSpu(@RequestBody SpuSaveInfoVo vo){

        if (vo!=null){
            spuInfoService.saveSpuInfoData(vo);
        }

        return Result.ok();
    }

    @GetMapping("spuImageList/{spuId}")
    public  Result getSpuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuImageService.lambdaQuery().eq(SpuImage::getSpuId, spuId).list();
        return Result.ok(list);
    }




}
